from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from users.models import User
from datetime import date, timedelta
from goods.models import GoodCategoryVisit
from .serializers import GoodsDayVisitSerializer


# 获取所有用户数
class TotalCountView(APIView):
    def get(self, request):
        count = User.objects.count()
        return Response({'count': count})


class DayIncrementView(APIView):
    def get(self, request):
        current_day = date.today()
        count = User.objects.filter(date_joined__gte=current_day).count()
        return Response({'count': count})


class DayActiveView(APIView):
    def get(self, request):
        count = User.objects.filter(last_login__gte=date.today()).count()
        return Response({'count': count})


class DayOrdersView(APIView):
    def get(self, request):
        count = User.objects.filter(orderinfo__create_time__gte=date.today()).count()
        return Response({'count': count})


class MonthIncrementView(APIView):
    def get(self, request):
        old_day = date.today() - timedelta(days=29)
        next_day = old_day + timedelta(days=1)

        a_list = []
        for i in range(30):
            count = User.objects.filter(date_joined__gte=old_day, date_joined__lt=next_day).count()
            a_list.append({'count': count, 'date': old_day})
            old_day += timedelta(days=1)
            next_day += timedelta(days=1)
        return Response(a_list)


class GoodsDayVisitView(ListAPIView):
    serializer_class = GoodsDayVisitSerializer

    def get_queryset(self):
        current_day = date.today()
        return GoodCategoryVisit.objects.filter(date__gte=current_day).all()
